package com.yuchen.ishop.buyer;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.yuchen.ishop.entity.BuyerTrace;
import com.yuchen.jaxrs.ResponseUtils;
import com.yuchen.jaxrs.Responses;
import com.yuchen.utils.security.PasswordUtils;
import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.ishop.entity.Buyer;
import com.yuchen.ishop.buyer.BuyerService;

import java.sql.Timestamp;
import java.util.Map;

@Transactional
@Path("buyerService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class BuyerServiceImpl implements BuyerService {

    private final static Logger LOG = Logger.getLogger(BuyerService.class);

    @Autowired
    private BaseDao dao;

    @GET
    @Path("get/{id}")
    @Transactional(readOnly = true)
    public Buyer getById(@PathParam("id") Long id) {
        return dao.fetch(Buyer.class, id);
    }

    @POST
    @Path("insert")
    public Buyer insert(Buyer buyer) {
        return dao.insert(buyer);
    }

    @POST
    @Path("update")
    public Responses update(Buyer buyer) {
        dao.updateIgnoreNull(buyer);
        return ResponseUtils.success();
    }

    @GET
    @Path("goodsTrace/{buyerId}/{goodsId}")
    public Responses goodsTrace(@PathParam("buyerId") Long buyerId, @PathParam("goodsId") Long goodsId) {
        BuyerTrace trace = dao.fetch(BuyerTrace.class, Cnd.where("buyerId","=",buyerId).and("goodsId","=",goodsId));
        if(null!=trace){
            trace.setTraceTime(new Timestamp(System.currentTimeMillis()));
            trace.setTraceCount(1+trace.getTraceCount());
            dao.updateIgnoreNull(trace);
        }else{
            trace = new BuyerTrace();
            trace.setGoodsId(goodsId);
            trace.setBuyerId(buyerId);
            trace.setTraceTime(new Timestamp(System.currentTimeMillis()));
            trace.setTraceCount(1l);
            dao.insert(trace);
        }
        return ResponseUtils.success();
    }

    @POST
    @Path("updatePwd")
    public Responses updatePwd(Map map){
        if (map != null && map.size() == 2 && map.get("id")!=null && map.get("password")!=null) {
            String password = (String) map.get("password");
            Map m = PasswordUtils.genMD5Str(password);
            Buyer buyer = new Buyer();
            buyer.setId(Long.valueOf(map.get("id").toString()));
            buyer.setSalt((String) m.get("salt"));
            buyer.setPassword((String) m.get("password"));
            dao.updateIgnoreNull(buyer);
            return ResponseUtils.success();
        }
        return ResponseUtils.error();
    }
}
